package com.shuang.link4;

public class SwapPairs {
    public ListNode swapPairs(ListNode head) {

        //判断
        if (head == null || head.next == null) {
            //无法交换直接返回
            return head;
        }

        //虚拟头节点
        ListNode vhead = new ListNode(0, head);

        ListNode curr = vhead;

        while (curr.next != null && curr.next.next != null) {

            //定义两个节点
            ListNode node1 = curr.next;
            ListNode node2 = curr.next.next;

            //画图表示即可
            //第一步
            curr.next = node2;

            //第二步
            node1.next = node2.next;

            //第三步
            node2.next = node1;

            //移动curr
            curr = curr.next.next;
        }

        return vhead.next;

    }
}
